package com.slz.crm.common.excellistener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.slz.crm.common.exiception.BaseException;
import com.slz.crm.common.untils.BaseUnit;
import com.slz.crm.common.untils.ValidationUtils;
import com.slz.crm.pojo.excel.CustomerContactExcel;
import com.slz.crm.pojo.entity.CustomerCompanyEntity;
import com.slz.crm.pojo.entity.CustomerContactEntity;
import com.slz.crm.server.constant.MessageConstant;
import com.slz.crm.server.mapper.CustomerCompanyMapper;
import lombok.Getter;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class CustomerContactListener implements ReadListener<CustomerContactExcel> {

    @Getter
    private final List<CustomerContactEntity> dataList = new ArrayList<>();
    private final Long ownerId;
    private final CustomerCompanyMapper customerCompanyMapper;

    public CustomerContactListener(CustomerCompanyMapper customerCompanyMapper) {
        this.ownerId = BaseUnit.getCurrentId();
        this.customerCompanyMapper = customerCompanyMapper;
    }

    @Override
    public void invoke(CustomerContactExcel customerContactExcel, AnalysisContext analysisContext) {


        //检查是否存在联系人姓名,电话,性别,公司
        if(customerContactExcel.getName() == null ||
                customerContactExcel.getMobile() == null ||
                customerContactExcel.getGender() == null ||
                (customerContactExcel.getCompanyId() == null && customerContactExcel.getCompanyName() == null)){
            throw new BaseException(String.format(MessageConstant.EXCEL_IS_ERROR, dataList.size() + 2));
        }

        //检查手机号和固定电话格式
        if((!ValidationUtils.isValidMobile(customerContactExcel.getMobile())) || ((customerContactExcel.getPhone() != null) && (!ValidationUtils.isValidPhone(customerContactExcel.getPhone())))){
            throw new BaseException(String.format(MessageConstant.EXCEL_IS_ERROR, dataList.size() + 2));
        }

        //转为Entity类型
        CustomerContactEntity customerContact = new CustomerContactEntity();
        BeanUtils.copyProperties(customerContactExcel, customerContact);

        //转化性别
        customerContact.setGender(Objects.equals(customerContactExcel.getGender(), "男") ? 1 : 2);

        if(customerContact.getCreatorId() == null){
            customerContact.setCreatorId(ownerId);
        }
        //判断是否通过公司名称添加公司
        CustomerCompanyEntity customerCompany;

        //判断是否填写公司ID，如果没有填写，则通过公司名称进行添加
        if(customerContactExcel.getCompanyId() == null){
            //通过公司名称添加公司
            customerCompany = customerCompanyMapper.selectOne(new LambdaQueryWrapper<CustomerCompanyEntity>()
                    .eq(CustomerCompanyEntity::getCompanyName, customerContactExcel.getCompanyName()));

            if(customerCompany == null){
                throw new BaseException(String.format(MessageConstant.EXCEL_COMPANY_IS_ERROR, dataList.size() + 2));
            }

            customerContact.setCompanyId(customerCompany.getId());
        }





        dataList.add(customerContact);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }



}
